﻿using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Tokenattributes;
using PanGu;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FortuneArterial.Site.BusinessLogicLayer.LunceneSearch.Analysis
{
    public class PanGuTokenizer 
        : Tokenizer
    {
        private static bool _Inited = false;
        private string _InputText;
        private static object _LockObj = new object();
        private bool _OriginalResult;
        private int _Position;
        private WordInfo[] _WordList;
        private IOffsetAttribute offsetAtt;
        private IPositionIncrementAttribute posIncrAtt;
        private ITermAttribute termAtt;
        private ITypeAttribute typeAtt;

        public PanGuTokenizer()
        {
            this._Position = -1;
            lock (_LockObj)
            {
                this.Init();
            }
        }

        public PanGuTokenizer(System.IO.TextReader input)
            : base(input)
        {
            this._Position = -1;
            lock (_LockObj)
            {
                this.Init();
            }
            this._InputText = base.input.ReadToEnd();
            if (string.IsNullOrEmpty(this._InputText))
            {
                char[] chArray = new char[0x400];
                int num = base.input.Read(chArray, 0, (int)chArray.Length);
                System.Text.StringBuilder builder = new System.Text.StringBuilder((int)chArray.Length);
                while (num > 0)
                {
                    builder.Append(chArray, 0, num);
                    num = input.Read(chArray, 0, (int)chArray.Length);
                }
                if (builder.Length > 0)
                {
                    this._InputText = builder.ToString();
                }
            }
            if (string.IsNullOrEmpty(this._InputText))
            {
                this._WordList = new WordInfo[0];
            }
            else
            {
                Segment segment = new Segment();
                System.Collections.Generic.ICollection<WordInfo> is2 = (System.Collections.Generic.ICollection<WordInfo>)segment.DoSegment(this._InputText);
                this._WordList = new WordInfo[is2.Count];
                is2.CopyTo(this._WordList, 0);
            }
        }

        public PanGuTokenizer(System.IO.TextReader input, bool originalResult)
            : this(input)
        {
            this._OriginalResult = originalResult;
        }

        public override bool IncrementToken()
        {
            this.ClearAttributes();
            Token token = this.Next();
            if (token != null)
            {
                this.termAtt.SetTermBuffer(token.Term);
                this.offsetAtt.SetOffset(token.StartOffset, token.EndOffset);
                this.typeAtt.Type = token.Type;
                return true;
            }
            this.End();
            return false;
        }

        private void Init()
        {
            InitPanGuSegment();
            this.termAtt = this.AddAttribute<ITermAttribute>();
            this.offsetAtt = this.AddAttribute<IOffsetAttribute>();
            this.posIncrAtt = this.AddAttribute<IPositionIncrementAttribute>();
            this.typeAtt = this.AddAttribute<ITypeAttribute>();
        }

        private static void InitPanGuSegment()
        {
            if (!_Inited)
            {
                Segment.Init();
                _Inited = true;
            }
        }

        public static void InitPanGuSegment(string fileName)
        {
            lock (_LockObj)
            {
                if (!_Inited)
                {
                    Segment.Init(fileName);
                    _Inited = true;
                }
            }
        }

        public Token Next()
        {
            if (this._OriginalResult)
            {
                string str = this._InputText;
                this._InputText = null;
                if (str == null)
                {
                    return null;
                }
                return new Token(str, 0, str.Length);
            }
            int num = 0;
            int position = 0;
            do
            {
                this._Position = (int)(this._Position + 1);
                if (this._Position >= this._WordList.Length)
                {
                    this._InputText = null;
                    return null;
                }
            }
            while (this._WordList[this._Position] == null);
            num = this._WordList[this._Position].Word.Length;
            position = this._WordList[this._Position].Position;
            return new Token(this._WordList[this._Position].Word, position, (int)(position + num));
        }

        public System.Collections.Generic.ICollection<WordInfo> SegmentToWordInfos(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return new LinkedList<PanGu.WordInfo>();
            }
            Segment segment = new Segment();
            return (System.Collections.Generic.ICollection<WordInfo>)segment.DoSegment(str);
        }
    }
}
